package com.mk.wardsystemserver.service.impl;

import com.mk.wardsystemserver.pojo.Department;
import com.mk.wardsystemserver.mapper.DepartmentMapper;
import com.mk.wardsystemserver.pojo.RespBean;
import com.mk.wardsystemserver.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 2022-02-24
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 获取所有部门
     *
     * @return
     */
    @Override
    public List<Department> getAllDepartments() {
        return departmentMapper.getAllDepartments(-1);
    }

    /**
     * 添加部门
     *
     * @param depart
     * @return
     */
    @Override
    public RespBean addDepart(Department depart) {
        depart.setEnabled(true);
        departmentMapper.addDepart(depart);
        if (1 == depart.getResult()) {
            return RespBean.success("添加成功", depart);
        } else {
            return RespBean.success("添加失败！");
        }
    }

    /**
     * 删除部门
     *
     * @param id
     * @return
     */
    @Override
    public RespBean deleteDepart(Integer id) {
        Department depart =new Department();
        depart.setId(id);
        departmentMapper.deleteDepart(depart);
        if(-2==depart.getResult()){
            return RespBean.error("该部门下还有子部门，删除失败！");
        }
        if(-1==depart.getResult()){
            return RespBean.error("该部门下还有员工，删除失败!");
        }
        if(1==depart.getResult()){
            return RespBean.success("删除成功!");
        }
        return RespBean.error("删除失败");
    }
}
